package com.asia5b.a5mall.shop.adapter

import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.InputType
import android.text.TextUtils
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.utils.WImageLoader
import com.asia5b.a5mall.middleware.utils.dddLog
import com.asia5b.a5mall.middleware.widget.BaseTextWatcher
import com.asia5b.a5mall.shop.R
import com.asia5b.a5mall.shop.fragment.ApplyStepBean
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder


/**
 * Created by zhangyuncai on 2020/6/29.
 */
class ApplyShopAgreeAdapter(list: ArrayList<ApplyStepBean>) :
        BaseMultiItemQuickAdapter<ApplyStepBean, BaseViewHolder>(list) {
    init {
        addItemType(ApplyStepBean.title, R.layout.item_apply_title)
        addItemType(ApplyStepBean.edittext, R.layout.item_apply_edittext)
        addItemType(ApplyStepBean.button, R.layout.item_apply_button)
        addItemType(ApplyStepBean.upload_img, R.layout.item_apply_uploadimg)
        addItemType(ApplyStepBean.select, R.layout.item_apply_select)
    }

    override fun convert(helper: BaseViewHolder, item: ApplyStepBean) {
        helper.setIsRecyclable(false)
        item.index = helper.layoutPosition
        when (item.type) {
            ApplyStepBean.title -> {
                helper.setText(R.id.tv_apply_name, item.name)
            }
            ApplyStepBean.edittext -> {
                helper.setText(R.id.tv_apply_name, item.name)
                val tv_apply_name = helper.getView<TextView>(R.id.tv_apply_name)
                val et_apply_value = helper.getView<EditText>(R.id.et_apply_value)
                et_apply_value.setHint(item.hint)
                et_apply_value.setText(item.value)
                et_apply_value.addTextChangedListener(object : BaseTextWatcher() {
                    override fun onTextChanged(charSequence: CharSequence?, start: Int, count: Int, after: Int) {
                        item.value = "${charSequence}"
                    }
                })
                when (item.name) {
                    ResString.get(R.string.公司电话) -> {
                        et_apply_value.inputType = InputType.TYPE_CLASS_PHONE
                        et_apply_value.filters = arrayOf<InputFilter>(object : LengthFilter(13) {})
                    }
                    ResString.get(R.string.员工总数) -> {
                        et_apply_value.inputType = InputType.TYPE_CLASS_NUMBER
                        et_apply_value.filters = arrayOf<InputFilter>(object : LengthFilter(10) {})
                    }
                    ResString.get(R.string.联系人电话) -> {
                        et_apply_value.inputType = InputType.TYPE_CLASS_NUMBER
                        et_apply_value.filters = arrayOf<InputFilter>(object : LengthFilter(11) {})
                    }
                    else -> {
                        et_apply_value.filters = arrayOf<InputFilter>(object : LengthFilter(50) {})
                    }
                }
                //去掉不是非必选的红星
                if (!item.hongxing) {
                    tv_apply_name.setCompoundDrawables(null, null, null, null)
                }
            }
            ApplyStepBean.button -> {
                helper.setText(R.id.bt_apply_name, item.name)
                helper.itemView.setOnClickListener(item.onClickListener!!)
            }
            ApplyStepBean.upload_img -> {
                val tv_apply_name = helper.getView<TextView>(R.id.tv_apply_name)
                helper.setText(R.id.tv_apply_name, item.name)
                helper.setText(R.id.tv_apply_hint, item.hint)

                //上传图片
                helper.getView<LinearLayout>(R.id.ll_upload_add)
                        .setOnClickListener {
                            item?.onClickListener?.onClick(it)
                        }
                //删除图片
                helper.getView<ImageView>(R.id.iv_upload_delete)
                        .setOnClickListener {
                            item?.deleteImgOnClickListener?.onClick(it)
                        }

                //有图片就展示
                helper.setGone(R.id.cons_upload_img_vessel, TextUtils.isEmpty(item.value))
                helper.setGone(R.id.ll_upload_add, !TextUtils.isEmpty(item.value))

                if (!TextUtils.isEmpty(item.value)) {
                    val iv_upload_img = helper.getView<ImageView>(R.id.iv_upload_img)
                    WImageLoader.displayRoundImageOption(item.value, iv_upload_img, 10)
                }
                //去掉不是非必选的红星
                if (!item.hongxing) {
                    tv_apply_name.setCompoundDrawables(null, null, null, null)
                }
            }
            ApplyStepBean.select -> {
                helper.setText(R.id.tv_apply_name, item.name)
                val tv_apply_value = helper.getView<TextView>(R.id.tv_apply_value)
                if (TextUtils.isEmpty(item.value)) {
                    tv_apply_value.setHint(item.hint)
                } else {
                    tv_apply_value.setText(item.value)
                }
                dddLog("item:${item}")
                helper.getView<LinearLayout>(R.id.ll_apply_select)
                        .setOnClickListener {
                            item.onClickListener?.onClick(it)
                        }
            }

        }
    }


}